package com.yuncheng.spcyApi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.entity.SpcyScheduleJobBasic;
import com.yuncheng.spcyApi.entity.SpcyScheduleJobLog;
import com.yuncheng.spcyApi.service.ISpcyScheduleJobBasicService;
import com.yuncheng.spcyApi.service.ISpcyScheduleJobLogService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.StartEndTimeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "审评查验-定时任务记录日志接口")
@RestController
@RequestMapping("/api/spzx-spcy/scheduleJobBasic")
public class ScheduleJobBasicController {
    private static final Logger log = LoggerFactory.getLogger(ScheduleJobBasicController.class);

    @Resource
    private ISpcyScheduleJobBasicService spcyScheduleJobBasicService;

    @Resource
    private ISpcyScheduleJobLogService spcyScheduleJobLogService;

    @Log(title = "查询定时任务记录")
    @ApiOperation(value = "查询定时任务记录")
    @GetMapping(value = "queryScheduleJobPage")
    public HttpResult<?> queryScheduleJobPage(SpcyScheduleJobBasic jobBasic,
                                                StartEndTimeVo startEndTimeVo,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SpcyScheduleJobBasic> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyScheduleJobBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(jobBasic.getFrwmc()),SpcyScheduleJobBasic::getFrwmc,jobBasic.getFrwmc());
//        wrapper.orderByDesc(SpcyScheduleJobBasic::getFzxsj);

        // 是否提交
        if (StringUtils.isNotBlank(startEndTimeVo.getFlag()) && startEndTimeVo.getFlag().equals("是")){
            wrapper.isNotNull(SpcyScheduleJobBasic::getFjssj);
        }else if (StringUtils.isNotBlank(startEndTimeVo.getFlag()) && startEndTimeVo.getFlag().equals("否")){
            wrapper.isNull(SpcyScheduleJobBasic::getFjssj);
        }
        if (startEndTimeVo.getStartTime() != null){
            wrapper.le(SpcyScheduleJobBasic::getCreateTime,startEndTimeVo.getStartTime());
        }
        if (startEndTimeVo.getEndTime() != null){
            wrapper.ge(SpcyScheduleJobBasic::getCreateTime,startEndTimeVo.getEndTime());
        }
        wrapper.orderByDesc(SpcyScheduleJobBasic::getCreateTime);
        IPage<SpcyScheduleJobBasic> jobBasicPage = spcyScheduleJobBasicService.page(page, wrapper);
        return HttpResult.ok(jobBasicPage);
    }

    @Log(title = "查询定时任务记录-详情")
    @ApiOperation(value = "查询定时任务记录-详情")
    @GetMapping(value = "queryJobLogPageById")
    public HttpResult<?> queryJobLogPageById(SpcyScheduleJobLog jobLog,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SpcyScheduleJobLog> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyScheduleJobLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(jobLog.getFzxrwid()),SpcyScheduleJobLog::getFzxrwid,jobLog.getFzxrwid());
        wrapper.like(StringUtils.isNotBlank(jobLog.getFpid()),SpcyScheduleJobLog::getFpid,jobLog.getFpid());

        wrapper.orderByDesc(SpcyScheduleJobLog::getCreateTime);
        IPage<SpcyScheduleJobLog> jobLogPage = spcyScheduleJobLogService.page(page, wrapper);
        return HttpResult.ok(jobLogPage);
    }

}
